/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin.module.commands;

import sh.grapendaal.tsuushin.annotation.Callback;
import sh.grapendaal.tsuushin.annotation.Module;
import sh.grapendaal.tsuushin.annotation.Permission;
import sh.grapendaal.tsuushin.annotation.Start;
import sh.grapendaal.tsuushin.annotation.Stop;
import sh.grapendaal.tsuushin.annotation.WireService;
import sh.grapendaal.tsuushin.message.IncomingMessage;
import sh.grapendaal.tsuushin.message.MessageType;
import sh.grapendaal.tsuushin.message.outgoing.OutgoingPrivateMessage;
import sh.grapendaal.tsuushin.service.BotService;
import sh.grapendaal.tsuushin.service.PermissionService;
import sh.grapendaal.tsuushin.tracking.Context;

@Module(author = "Dik Grapendaal", version = "0.0.1")
@Permission(names = { "commands_moderator" })
public class Commands {
	
	@WireService
	private BotService botService;
	
	@WireService
	private PermissionService permissionService;
	
	@Callback(type = MessageType.CHANNEL_MESSAGE)
	public void onPrivateMessage(Context context, IncomingMessage message) {
		sh.grapendaal.tsuushin.message.incoming.IncomingPrivateMessage incoming = (sh.grapendaal.tsuushin.message.incoming.IncomingPrivateMessage) message;

		if (incoming.getContent().startsWith("!")) {
			if (incoming.getContent().startsWith("!whereami")) {
				OutgoingPrivateMessage outgoing = new OutgoingPrivateMessage();
				outgoing.setDestination(incoming.getChannel());
				outgoing.setMessage("You're in " + incoming.getChannel() + "!");
				context.getBot().send(outgoing);
			}
//			} else if (incoming.getContent().startsWith("!lequit")) {
//				OutgoingQuitMessage outgoing = new OutgoingQuitMessage();
//				outgoing.setMessage("kbie");
//				context.getBot().send(outgoing);
//			}
		}
	}

	@Start
	public void onModuleStart() {
		if (botService != null) {
			System.out.println("Commands.onModuleStart(): " + botService.getClass());
		} else {
			System.out.println("Bot Service null. :(");
		}
		
		if (permissionService != null) {
			System.out.println("Commands.onModuleStart(): " + permissionService.getClass());
		} else {
			System.out.println("Permission Service null. :(");
		}
	}

	@Stop
	public void onModuleStop() {
		if (botService != null) {
			System.out.println("Bot Service still available before shutting down: " + botService.getClass());
		} else {
			System.out.println("Bot Service not available, what? :(");
		}
		
		if (permissionService != null) {
			System.out.println("Permission Service still available before shutting down: " + permissionService.getClass());
		} else {
			System.out.println("Permission not available, what? :(");
		}
	}
}
